import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {Router} from '@angular/router';
import {HttpClient} from '@angular/common/http';
import {CudTableService} from '../../public/serve/cud-table/cud-table.service';
import {Service} from '../../server/server';
import {RemoveTableService} from '../../public/serve/remove-table/remove-table.service';
import {HintsService} from '../../public/serve/hints/hints.service';
import {Pagination} from '../../public/serve/pcrudInfo';
import { PushedTableComponent } from '../pushed/pushed-table/pushed-table.component';
declare var $:any;

@Component({
  selector: 'app-pushed',
  templateUrl: './pushed.component.html',
  styleUrls: ['./pushed.component.css']
})

export class PushedComponent implements OnInit {

    total      : number = 0;
    naturalNum : number = 0;
    startTime : String = '';
    endTime : String = '';
    details    : object;
    isSelectAll : boolean = false;
    isChose     : boolean = false;
    right: any = {
        "systemPushed": {},
    };

    constructor( public removeTableService: RemoveTableService,private http: HttpClient, private hintsService: HintsService, private server: Service, private router: Router, private tableListService: CudTableService) {
        this.right = $.extend(this.right, server.getRight());
    }
    //导入table
    @ViewChild("table")
    table: PushedTableComponent;

    @Input() searchData: any;
    @Input() tableNumber: number;

    ngOnInit() {
        this.table.getTabList(1, 20, '0', this.searchData);
    }

    public tabVal: any;
    public rolesData: any[];

    public totalItems: number;
    // 当前页（默认为第一页）
    private pageNumber: number = 1;
    // 每页显示多少条数据（默认每页显示20条）
    private pageSize: number = 20;
    // id
    private id: string = '0';
    // 分页
    private pagination: Pagination;
    isLoadTableFromPaginationHandler(pagination: Pagination) {
        this.pagination = pagination;

        this.pageNumber = this.pagination[0].pageNumber;
        this.pageSize = this.pagination[0].pageSize;
        this.id = this.pagination[0].id;

        this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;

        this.table.getTabList(this.pageNumber, this.pageSize, this.id, this.searchData);
    }


    /**
     * 重置表格复选框、选中的ID
     */
    private _cancelChecked() {
        this.isChose = this.isSelectAll = false;
        this.removeTableService._clearId('scopesId');
    }

    /**
     * 批量启用或禁用
     * @param val
     */
    private _status(val:any){
        //TODO::主题词表缺少批量启用禁止接口 20180920
        this.removeTableService
            .confirmRemove('ids',this.server.baseUrl + 'Service/ThemeWords/updateThemeTable','status',val)
            .then(res => {
                if(res.result_code == "0000"){
                    this.table.getTabList(this.pageNumber, this.pageSize, this.id, this.searchData);
                }
                this.hintsService._operationHints(res.message,res.result_code);
                return;
            })
            .catch(err =>{
                this.hintsService._operationHints('error','error');
                return;
            });
    }

    //查询
    search() {
        this.table.getTabList(1, 20, '0', this.searchData);
    }
    //响应搜索
    changesearch() {
        this.search();
    }

    /**
     * 禁用
     */
    _disable(status) {
        this._status(status);
    }

    /**
     * 启用
     */
    _enable(status) {
        this._status(status);
    }

    /**
     * 全选
     * @param bool
     * @param tabVal
     */
    _selectAll(bool, tabVal) {
        this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'scopesId');
        if (this.isSelectAll == false) {
            this.table.getTabList(1, 20, '0', this.searchData);
        }
    }

    /**
     * 单选（多选）
     * @param bool
     * @param scopesId
     * @param length
     */
    _chose(bool, scopesId, length) {
        this.isSelectAll = this.removeTableService.removeChose(bool, scopesId, length)['bool'];
    }

    ParentAdd(result_code: string) {
        this.table.getTabList(this.pageNumber, this.pageSize, '0', this.searchData);
    }

    ParentEdit(result_code: string) {
        this.table.getTabList(this.pageNumber, this.pageSize, '0', this.searchData);
    }

    ParentDelete(result_code: string) {
        this.table.getTabList(this.pageNumber, this.pageSize, '0', this.searchData);
    }

    ParentList(totalItems: number) {
        this.totalItems = totalItems;
    }

    ParentDetails(details: any) {
        this.details = details;
    }

    // 删除按钮
    isSelect: boolean = true;
    isSelectFromTableHandler(isSelect: boolean) {
        this.isSelect = isSelect;
    }



}
